package com.neuedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Drug;
import com.neuedu.entity.Level;
import com.neuedu.service.DrugService;
import com.neuedu.vo.DrugCondition;
import com.neuedu.vo.DrugRegCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api("药品管理")
@RestController
@RequestMapping("/drug")
@CrossOrigin
public class DrugController {

    @Autowired
    private DrugService drugService;

    @ApiOperation("药品列表")
    @GetMapping("/list")
    public ServerResponse list(DrugCondition drugCondition) {

        //调用业务方法
        Page<Drug> drugPage = drugService.getDrugList(drugCondition);
        //返回分页对象
        return ServerResponse.success("").data("drugPage", drugPage);
    }

    @ApiOperation("添加药品")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Drug drug) {

        if(StringUtils.isEmpty(drug.getDrugName()))  {
            return ServerResponse.error("药品不能为空");
        }
        if(StringUtils.isEmpty(drug.getDrugCode()))  {
            return ServerResponse.error("药品编码不能为空");
        }

        if(drug.getDrugName().length()>10){
            return ServerResponse.error("药品长度不能超过10个字符");
        }
        if(drugService.save(drug)){
            return ServerResponse.success("添加药品成功");
        }else{
            return ServerResponse.error("添加药品失败");
        }
    }

    @ApiOperation("根据编号获取药品信息")
    @GetMapping("/get/{drugId}")
    public ServerResponse get(@PathVariable int drugId) {

        Drug drug = drugService.getById(drugId);

        if(drug!=null){
            return ServerResponse.success("").data("drug", drug);
        }else{
            return ServerResponse.error("获取药品信息失败");
        }
    }

    @ApiOperation("修改药品信息")
    @PutMapping("/modify")
    public ServerResponse modify(@RequestBody Drug drug) {

        if(StringUtils.isEmpty(drug.getDrugName()) && StringUtils.isEmpty(drug.getDrugCode())
                && StringUtils.isEmpty(drug.getDrugFormat()) && StringUtils.isEmpty(drug.getDrugFactory())
        && StringUtils.isEmpty(drug.getDrugPrice()) && StringUtils.isEmpty(drug.getDrugType()))  {
            return ServerResponse.error("所修改的药品信息不能为空");
        }

        if(drug.getDrugName().length()>10){
            return ServerResponse.error("药品名称的长度不能超过10个字符");
        }

        if(drugService.updateById(drug)){
            return ServerResponse.success("修改药品信息成功");
        }else{
            return ServerResponse.error("修改挂号级别失败");
        }
    }

    @ApiOperation("删除药品")
    @DeleteMapping("/remove/{drugId}")
    public ServerResponse remove(@PathVariable int drugId) {

        if(drugService.removeById(drugId)){
            return ServerResponse.success("删除药品成功");
        }else{
            return ServerResponse.error("删除药品失败");
        }
    }

    @ApiOperation("批量删除药品")
    @DeleteMapping("/deleteBatch")
    public ServerResponse deleteBatch(String drugIds) {

        if(drugIds == ""){
            return ServerResponse.error("未选中要删除的药品列表");
        }
        if(drugService.deleteBatch(drugIds)){
            return ServerResponse.success("批量删除药品成功");
        }else{
            return ServerResponse.error("批量删除药品失败");
        }
    }

    @ApiOperation("药品列表---开具处方时使用")
    @GetMapping("/listAll")
    public ServerResponse list(DrugRegCondition drugRegCondition) {

        //调用业务方法
        List<Drug> drugList = drugService.query()
                .like("drug_name", drugRegCondition.getDrugName())
                .like("drug_code", drugRegCondition.getDrugCode())
                .list();

        //返回药品列表
        return ServerResponse.success("").data("drugList", drugList);
    }
}
